package com.c201801020208.ybl.User;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.c201801020208.ybl.Company.Activity.AboutActivity;
import com.c201801020208.ybl.Login.Activity.LoginActivity;
import com.c201801020208.ybl.R;
import com.c201801020208.ybl.Tool.CircleImageView;
import com.c201801020208.ybl.User.Activity.addWanted;
import com.c201801020208.ybl.User.Activity.help;
import com.c201801020208.ybl.User.Activity.homeActivity;
import com.c201801020208.ybl.User.Activity.intentionActivity;
import com.c201801020208.ybl.model.impl.RTCEntranceActivity;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMManager;

import org.xutils.BuildConfig;
import org.xutils.DbManager;
import org.xutils.x;

import static android.content.Context.MODE_PRIVATE;

public class PersonalFragment extends Fragment {
    CircleImageView head;
    RelativeLayout button;
    TextView Username;
    LoginActivity loginActivity;
    DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
            .setDbName("test.db")
            // 不设置dbDir时, 默认存储在app的私有目录.
            // .setDbDir(new File("/sdcard")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了.
            .setDbVersion(2)
            .setDbOpenListener(new DbManager.DbOpenListener() {
                @Override
                public void onDbOpened(DbManager db) {
                    // 开启WAL, 对写入加速提升巨大
                    db.getDatabase().enableWriteAheadLogging();
                }
            })
            .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                @Override
                public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                    // TODO: ...
                    // db.addColumn(...);
                    // db.dropTable(...);
                    // ...
                    // or
                    // db.dropDb();
                }
            });


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_personal, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        x.Ext.init(getActivity().getApplication());
        x.Ext.setDebug(BuildConfig.DEBUG);// 是否输出debug日志, 开启debug会影响性能.


        //用户名
        Username = getActivity().findViewById(R.id.textView11);
        String name = getActivity().getIntent().getStringExtra("username");
        Username.setText(name);

        //设置头像
        head = getActivity().findViewById(R.id.imageView71);

//        //decode base64 string to image
//        byte[] imageBytes = Base64.decode(loginActivity.image, Base64.DEFAULT);
//        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
//        head.setImageBitmap(decodedImage);


        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
                galleryIntent.setType("image/*");//图片
                startActivityForResult(galleryIntent, 0);
            }
        });

//                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
//                galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
//                galleryIntent.setType("image/*");//图片
//                startActivityForResult(galleryIntent, 0);
//                DbManager db = null;
//                try {
//                    db = x.getDb(daoConfig);
//                    List<PUser> users = new ArrayList<>();
//                    users= db.selector(PUser.class).orderBy("username", true).limit(1000).where("username","=",Username).findAll();
//                    for (PUser user : users){
//                        Log.i("testdb","test" + user);
//                        //encode image to base64 string
//                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                        byte[] imageBytes = baos.toByteArray();
//                        //decode base64 string to image
//                        imageBytes = Base64.decode(user.Image, Base64.DEFAULT);
//                        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
//                        head.setImageBitmap(decodedImage);
//                    }
//
//                } catch (DbException e) {
//                    e.printStackTrace();
//                    return;
//                }


        //视频面试
        ImageView imageView = getActivity().findViewById(R.id.imageView8);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RTCEntranceActivity.class);
                startActivity(intent);
            }
        });

        //发布简历
        RelativeLayout button3 = getActivity().findViewById(R.id.jianli);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), addWanted.class);
                intent.putExtra("username", name);
                startActivity(intent);
            }
        });

        //个人主页
        RelativeLayout button4 = getActivity().findViewById(R.id.safeLayout);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), homeActivity.class);
                startActivity(intent);
            }
        });

        //帮助
        RelativeLayout button5 = getActivity().findViewById(R.id.helpLayout);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), help.class);
                startActivity(intent);
            }
        });

        RelativeLayout button6 = getActivity().findViewById(R.id.infor);
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), intentionActivity.class);
                startActivity(intent);
            }
        });

        //About
        RelativeLayout button2 = getActivity().findViewById(R.id.abortLayout);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
            }
        });

        //退出登录
        button = getActivity().findViewById(R.id.quitLayout);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                V2TIMManager.getInstance().logout(new V2TIMCallback() {
                    @Override
                    public void onError(int code, String desc) {
                        SharedPreferences preferences = getActivity().getSharedPreferences("user", MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.clear();
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                    }

                    @Override
                    public void onSuccess() {
                        SharedPreferences preferences = getActivity().getSharedPreferences("user", MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.clear();
                        editor.commit();
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                    }
                });
            }
        });


    }

    //设置头像
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 0 && resultCode == -1) {
            Uri uri = data.getData();
            head.setImageURI(uri);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
